﻿namespace Chain.IDAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Runtime.InteropServices;
    using System.Text;

    public class SmsTemplate
    {
        public int Add(Chain.Model.SmsTemplate model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into SmsTemplate(");
            builder.Append("TemplateName,TemplateContent)");
            builder.Append(" values (");
            builder.Append("@TemplateName,@TemplateContent)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@TemplateName", SqlDbType.NVarChar, 100), new SqlParameter("@TemplateContent", SqlDbType.NVarChar, 0x3e8) };
            cmdParms[0].Value = model.TemplateName;
            cmdParms[1].Value = model.TemplateContent;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Delete(int TemplateID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from SmsTemplate ");
            builder.Append(" where TemplateID=@TemplateID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@TemplateID", SqlDbType.Int, 4) };
            cmdParms[0].Value = TemplateID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string TemplateIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from SmsTemplate ");
            builder.Append(" where TemplateID in (" + TemplateIDlist + ")  ");
            return (DbHelperSQL.ExecuteSql(builder.ToString()) > 0);
        }

        public bool Exists(int TemplateID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from SmsTemplate");
            builder.Append(" where TemplateID=@TemplateID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@TemplateID", SqlDbType.Int, 4) };
            cmdParms[0].Value = TemplateID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select TemplateID,TemplateName,TemplateContent ");
            builder.Append(" FROM SmsTemplate ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" TemplateID,TemplateName,TemplateContent ");
            builder.Append(" FROM SmsTemplate ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM ( ");
            builder.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                builder.Append("order by T." + orderby);
            }
            else
            {
                builder.Append("order by T.TemplateID desc");
            }
            builder.Append(")AS Row, T.*  from SmsTemplate T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                builder.Append(" WHERE " + strWhere);
            }
            builder.Append(" ) TT");
            builder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListSP(int PageSize, int PageIndex, out int resCount, params string[] strWhere)
        {
            string tableName = "SmsTemplate";
            string[] column = new string[] { "*" };
            string[] condition = strWhere;
            int recordCount = 1;
            DataSet set = DbHelperSQL.GetTable(tableName, column, condition, "TemplateID", true, PageSize, PageIndex, recordCount);
            resCount = int.Parse(set.Tables[1].Rows[0][0].ToString());
            return set;
        }

        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("TemplateID", "SmsTemplate");
        }

        public Chain.Model.SmsTemplate GetModel(int TemplateID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 TemplateID,TemplateName,TemplateContent from SmsTemplate ");
            builder.Append(" where TemplateID=@TemplateID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@TemplateID", SqlDbType.Int, 4) };
            cmdParms[0].Value = TemplateID;
            Chain.Model.SmsTemplate template = new Chain.Model.SmsTemplate();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count <= 0)
            {
                return null;
            }
            if ((set.Tables[0].Rows[0]["TemplateID"] != null) && (set.Tables[0].Rows[0]["TemplateID"].ToString() != ""))
            {
                template.TemplateID = int.Parse(set.Tables[0].Rows[0]["TemplateID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["TemplateName"] != null) && (set.Tables[0].Rows[0]["TemplateName"].ToString() != ""))
            {
                template.TemplateName = set.Tables[0].Rows[0]["TemplateName"].ToString();
            }
            if ((set.Tables[0].Rows[0]["TemplateContent"] != null) && (set.Tables[0].Rows[0]["TemplateContent"].ToString() != ""))
            {
                template.TemplateContent = set.Tables[0].Rows[0]["TemplateContent"].ToString();
            }
            return template;
        }

        public int GetRecordCount(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) FROM SmsTemplate ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public int Update(Chain.Model.SmsTemplate model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update SmsTemplate set ");
            builder.Append("TemplateName=@TemplateName,");
            builder.Append("TemplateContent=@TemplateContent");
            builder.Append(" where TemplateID=@TemplateID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@TemplateName", SqlDbType.NVarChar, 100), new SqlParameter("@TemplateContent", SqlDbType.NVarChar, 0x3e8), new SqlParameter("@TemplateID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.TemplateName;
            cmdParms[1].Value = model.TemplateContent;
            cmdParms[2].Value = model.TemplateID;
            return DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms);
        }
    }
}

